package com.glela.activ.dao.impl;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.glela.activ.dao.OActivConditionSalesAmountDao;
import com.glela.activ.dao.OActivContentExpressDao;
import com.glela.activ.dao.OActivContentGiftDao;
import com.glela.activ.dao.OActivContentMoneyDao;
import com.glela.activ.mapper.OActivConditionSalesAmountMapper;
import com.glela.activ.service.domain.info.OActivInfoConditionSaleAmountVo;
import com.glela.activ.service.domain.info.OActivInfoContentMoneyVo;

@Repository
public class OActivConditionSalesAmountDaoImpl implements OActivConditionSalesAmountDao {
	private static Logger LOGGER = LoggerFactory.getLogger(OActivConditionSalesAmountDaoImpl.class);

	@Autowired
	private OActivConditionSalesAmountMapper	oActivConditionSalesAmountMapper;
	@Autowired
	private OActivContentExpressDao				oActivContentExpressDao;
	@Autowired
	private OActivContentMoneyDao				oActivContentMoneyDao;
	@Autowired
	private OActivContentGiftDao				oActivContentGiftDao;

	@Override
	public List<OActivInfoConditionSaleAmountVo> selectByActivId(long activId) {
		List<OActivInfoConditionSaleAmountVo> activConditionSaleAmounts = oActivConditionSalesAmountMapper.selectByActivId(activId);
		if (CollectionUtils.isNotEmpty(activConditionSaleAmounts)) {
			//活动内容
			for (OActivInfoConditionSaleAmountVo oActivConditionSaleAmountVo : activConditionSaleAmounts) {
				if (oActivConditionSaleAmountVo.getIsDouble() == null) {
					oActivConditionSaleAmountVo.setIsDouble(0);
				}
				//运费
				oActivConditionSaleAmountVo.setExpress(oActivContentExpressDao.queryByConditionId(oActivConditionSaleAmountVo.getId()));
				//立减
				OActivInfoContentMoneyVo conditionMoney = oActivContentMoneyDao.queryByConditionId(oActivConditionSaleAmountVo.getId());
				oActivConditionSaleAmountVo.setMoney(conditionMoney);
				if (conditionMoney != null) {//如果满减条件和满减内容都翻倍
					if (conditionMoney.getIsDouble() == null || oActivConditionSaleAmountVo.getIsDouble() == 0) {
						conditionMoney.setIsDouble(0);
					}
				}
				//赠品礼包
				oActivConditionSaleAmountVo.setGifts(oActivContentGiftDao.queryByConditionId(oActivConditionSaleAmountVo.getId()));
				if (oActivConditionSaleAmountVo.getExpress() == null && conditionMoney == null && oActivConditionSaleAmountVo.getGifts() == null) {
					LOGGER.error("活动{}条件{}下未设置内容", activId, oActivConditionSaleAmountVo.getId());
					throw new IllegalArgumentException("活动未设置内容");
				}
			}
		}
		return activConditionSaleAmounts;
	}
}
